/*
 * Created on Dec 6, 2005
 *
 */
package org.mushroomdb.query.impl;

import org.mushroomdb.catalog.Catalog;
import org.mushroomdb.catalog.Table;
import org.mushroomdb.engine.EvaluationEngine;
import org.mushroomdb.query.Query;

/**
 * @author mchiodi
 *
 */
public class DropTableQuery extends Query {
	private Table table;

	/**
	 * Constructor
	 */
	public DropTableQuery(Table table) {
		super();
		this.table = table;
	}

	/**
	 * @see org.mushroomdb.query.Query#execute(org.mushroomdb.engine.EvaluationEngine)
	 */
	public Object execute(EvaluationEngine evaluationEngine) {
		
		Catalog catalog = Catalog.getInstance();
		boolean result = catalog.dropTable(this.table);
		return new Boolean(result);
		
		// TODO: eliminar esto after testing del DELETE
//		/** Free table's pages */
//		FileManager fileManager = FileManager.getInstance();
//		Iterator iterator = this.table.getPages();
//		try {
//			while (iterator.hasNext()) {
//				Page page = (Page) iterator.next();
//				fileManager.removePage(page);
//			}
//		} catch (IOException e) {
//			throw new RDBMSRuntimeException(e);
//		}
//		
//		/** Get Table ID for register deletion */
//		Table sysTable = Catalog.getInstance().getTableByName(CatalogHelper.SYS_TABLE_TABLE_NAME);
//		Column idColumn = sysTable.getColumn(CatalogHelper.ST_ID_COLUMN_NAME);
//		Column nameColumn = sysTable.getColumn(CatalogHelper.ST_NAME_COLUMN_NAME);
//		SelectionQuery selectionQuery = new SelectionQuery();
//		Criteria criteria = selectionQuery.getNewCriteria();
//		criteria.addEqualsTo(nameColumn,this.table.getName());
//		selectionQuery.addProjection(idColumn);
//		ResultSet resultSet = evaluationEngine.executeQuery(selectionQuery);
//		Long idTable = null;
//		try {
//			try {
//				while (resultSet.hasNext()) {
//					Register register = (Register) resultSet.next();
//					idTable = (Long) register.getValue(1);
//				}
//			} finally {
//				resultSet.close();
//			}
//		} catch (EvaluationEngineException e) {
//			throw new RDBMSRuntimeException(e);
//		}
//		
//		/** Remove SYS_PAGE entries */
//		Table sysPage = Catalog.getInstance().getTableByName(CatalogHelper.SYS_PAGE_TABLE_NAME);
//		Column idTableColumn = sysPage.getColumn(CatalogHelper.SP_ID_TABLE_COLUMN_NAME);
//		selectionQuery = new SelectionQuery();
//		criteria = selectionQuery.getNewCriteria();
//		criteria.addEqualsTo(idTableColumn,idTable);
//		DeletionQuery deletionQuery = new DeletionQuery(selectionQuery);
//		evaluationEngine.executeUpdate(deletionQuery);
//		
//		/** Remove SYS_COLUMN entries */
//		Table sysColumn = Catalog.getInstance().getTableByName(CatalogHelper.SYS_COLUMN_TABLE_NAME);
//		idTableColumn = sysColumn.getColumn(CatalogHelper.SC_ID_TABLE_COLUMN_NAME);
//		selectionQuery = new SelectionQuery();
//		criteria = selectionQuery.getNewCriteria();
//		criteria.addEqualsTo(idTableColumn,idTable);
//		deletionQuery = new DeletionQuery(selectionQuery);
//		evaluationEngine.executeUpdate(deletionQuery);
//		
//		/** Remove SYS_TABLE register */
//		idColumn = sysTable.getColumn(CatalogHelper.ST_ID_COLUMN_NAME);
//		selectionQuery = new SelectionQuery();
//		criteria = selectionQuery.getNewCriteria();
//		criteria.addEqualsTo(idColumn,idTable);
//		deletionQuery = new DeletionQuery(selectionQuery);
//		evaluationEngine.executeUpdate(deletionQuery);
//		
//		return Boolean.TRUE;
	}

}
